﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using SMS.BaseClass;

namespace SMS.GoodsManage
{
    public partial class frmStockOutManage : Form
    {
        #region 常量变量枚举类型定义
        /// <summary>
        /// Form Load Flag
        /// </summary>
        private Boolean blnLoadFlg = false;

        /// <summary>
        /// 画面处理类型
        ///     0 - 新建
        ///     1 - 变更
        ///     2 - 删除
        /// </summary>
        private static int intFormType = -1;

        /// <summary>
        /// 出库单ID
        /// </summary>
        private string strSTOCK_OUT_ID = string.Empty;

        DataTable dtT_STOCK_OUT_HEAD = null;
        DataTable dtT_STOCK_OUT_DETAIL = null;
        DataView dvStockOutGoods = new DataView();
        DataView dvStockOutGoodsDetail = new DataView();

        SMS.BaseClass.DataCon datacon = new SMS.BaseClass.DataCon();
        SMS.BaseClass.DataOperate doperate = new SMS.BaseClass.DataOperate();
        SMS.BaseClass.Utils utils = new BaseClass.Utils();

        private enum GOODS_COL_NAME : uint
        {
            GOODS_CODE = 0,
            GOODS_NAME,
            GOODS_MADEPLACE
        }

        private enum BARCODE_COL_NAME : uint
        {
            GOODS_CODE = 0,
            GOODS_NAME,
            GOODS_MADEPLACE,
            GOODS_BARCODE
        }
        #endregion 常量变量枚举类型定义

        #region 构造函数
        /// <summary>
        /// 无参构造函数
        /// </summary>
        public frmStockOutManage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 有参构造函数
        /// </summary>
        /// <param name="intType"></param>
        /// <param name="strId"></param>
        public frmStockOutManage(int intType, string strId)
        {
            // 接收页面处理类型
            intFormType = intType;
            // 接收出库单号
            this.strSTOCK_OUT_ID = strId;

            InitializeComponent();
        }
        #endregion 构造函数

        #region 事件

        #region Form Load
        /// <summary>
        /// Form_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmStockOutManage_Load(object sender, EventArgs e)
        {
            this.blnLoadFlg = true;

            // 设置画面状态和数据
            this.SetForm();

            this.blnLoadFlg = false;
        }
        #endregion Form Load

        #region 保存按钮点击事件
        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string strInsertSQL = string.Empty;
            string strDeleteSQL = string.Empty;
            string strUpdateSQL = string.Empty;

            // 出库单ID
            string strSTOCK_OUT_ID = this.tbxSTOCK_OUT_ID.Text;
            // 出库日期
            string strSTOCK_OUT_DATE = this.dtpSTOCK_OUT_DATE.Value.ToShortDateString();
            // 操作者
            string strSTOCK_OUT_OPERATER = this.tbxSTOCK_OUT_OPERATER.Text;
            // 确认者
            string strSTOCK_OUT_CONFIRMER = this.tbxSTOCK_OUT_CONFIRMER.Text;
            // 备注
            string strSTOCK_OUT_MEMO = this.tbxSTOCK_OUT_MEMO.Text;

            // 货物编号
            string strGOODS_CODE = string.Empty;
            string strOldGOODS_CODE = string.Empty;
            // 货物序列号
            string strGOODS_BARCODE = string.Empty;
            string strOldGOODS_BARCODE = string.Empty;

            // 提示信息
            string strMessage = string.Empty;

            try
            {
                #region 校验Head
                // 操作者名称
                if (this.tbxSTOCK_OUT_OPERATER.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("操作者名称必须输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbxSTOCK_OUT_OPERATER.Focus();
                    return;
                }

                // 确认者名称
                if (this.tbxSTOCK_OUT_CONFIRMER.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("确认者名称必须输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbxSTOCK_OUT_CONFIRMER.Focus();
                    return;
                }
                #endregion 校验Head

                #region 校验明细
                // 操作者名称
                if (this.tbxSTOCK_OUT_OPERATER.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("操作者名称必须输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbxSTOCK_OUT_OPERATER.Focus();
                    return;
                }

                // 确认者名称
                if (this.tbxSTOCK_OUT_CONFIRMER.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("确认者名称必须输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tbxSTOCK_OUT_CONFIRMER.Focus();
                    return;
                }
                #endregion 校验明细

                #region 保存Head表
                // 新建
                if (intFormType == 0)
                {
                    strInsertSQL += "INSERT INTO T_STOCK_OUT_HEAD (STOCK_OUT_ID,STOCK_OUT_DATE,STOCK_OUT_OPERATER,STOCK_OUT_CONFIRMER,STOCK_OUT_MEMO) VALUES('" +
                                   strSTOCK_OUT_ID + "','" + strSTOCK_OUT_DATE + "','" + strSTOCK_OUT_OPERATER + "','" + strSTOCK_OUT_CONFIRMER + "','" + strSTOCK_OUT_MEMO + "');";

                }
                // 变更
                else if (intFormType == 1)
                {
                    strUpdateSQL += "UPDATE T_STOCK_OUT_HEAD SET STOCK_OUT_DATE='" + strSTOCK_OUT_DATE
                                   + "',STOCK_OUT_OPERATER='" + strSTOCK_OUT_OPERATER + "',STOCK_OUT_CONFIRMER='" + strSTOCK_OUT_CONFIRMER
                                   + "',STOCK_OUT_MEMO='" + strSTOCK_OUT_MEMO
                                   + "' WHERE STOCK_OUT_ID='" + strSTOCK_OUT_ID + "';";
                }
                // 删除
                else if (intFormType == 2)
                {
                    strDeleteSQL += "DELETE FROM T_STOCK_OUT_HEAD WHERE STOCK_OUT_ID='" + strSTOCK_OUT_ID + "';";

                    if (this.dtT_STOCK_OUT_DETAIL.Rows.Count != 0)
                    {
                        for (int i = 0; i < this.dtT_STOCK_OUT_DETAIL.Rows.Count; i++)
                        {
                            this.dtT_STOCK_OUT_DETAIL.Rows[i].Delete();
                        }
                    }
                }
                #endregion

                #region 保存 Detail表
                DataTable dtDetail = this.dtT_STOCK_OUT_DETAIL.GetChanges();

                if (dtDetail != null && dtDetail.Rows.Count > 0)
                {
                    for (int i = 0; i < dtDetail.Rows.Count; i++)
                    {
                        switch (dtDetail.Rows[i].RowState)
                        {
                            case DataRowState.Added:
                                strGOODS_CODE = dtDetail.Rows[i]["GOODS_CODE"].ToString();
                                strGOODS_BARCODE = dtDetail.Rows[i]["GOODS_BARCODE"].ToString();

                                if (strGOODS_CODE.Trim() != string.Empty && strGOODS_BARCODE.Trim() != string.Empty)
                                {
                                    strInsertSQL += "INSERT INTO T_STOCK_OUT_DETAIL (GOODS_BARCODE,GOODS_CODE,STOCK_OUT_ID) VALUES('"
                                    + strGOODS_BARCODE + "','" + strGOODS_CODE + "','" + strSTOCK_OUT_ID + "');";
                                }

                                break;

                            case DataRowState.Modified:
                                strGOODS_CODE = dtDetail.Rows[i]["GOODS_CODE"].ToString();
                                strGOODS_BARCODE = dtDetail.Rows[i]["GOODS_BARCODE"].ToString();

                                //strOldGOODS_CODE = dtDetail.Rows[i]["GOODS_CODE", DataRowVersion.Original].ToString();
                                strOldGOODS_BARCODE = dtDetail.Rows[i]["GOODS_BARCODE", DataRowVersion.Original].ToString();

                                // 先删除
                                strDeleteSQL += "DELETE FROM T_STOCK_OUT_DETAIL WHERE GOODS_BARCODE='" + strOldGOODS_BARCODE + "';";

                                // 后插入
                                //strUpdateSQL += "UPDATE T_STOCK_OUT_DETAIL SET GOODS_CODE='" + strGOODS_CODE
                                //    + "',GOODS_BARCODE='" + strGOODS_BARCODE
                                //    + "' WHERE STOCK_OUT_ID='" + strSTOCK_OUT_ID
                                //    + "' AND GOODS_CODE = '" + strOldGOODS_CODE
                                //    + "' AND GOODS_BARCODE = '" + strOldGOODS_BARCODE + "';";

                                strInsertSQL += "INSERT INTO T_STOCK_OUT_DETAIL (GOODS_BARCODE,GOODS_CODE,STOCK_OUT_ID) VALUES('"
                                 + strGOODS_BARCODE + "','" + strGOODS_CODE + "','" + strSTOCK_OUT_ID + "');";

                                break;

                            case DataRowState.Deleted:
                                strGOODS_BARCODE = dtDetail.Rows[i]["GOODS_BARCODE", DataRowVersion.Original].ToString();
                                strDeleteSQL += "DELETE FROM T_STOCK_OUT_DETAIL WHERE GOODS_BARCODE='" + strGOODS_BARCODE + "';";
                                break;
                        }
                    }
                }
                #endregion 保存 Detail表

                #region 编辑提示信息
                if (intFormType == 0)
                {
                    strMessage = "出库单新建成功！";// + ( (dtDetail == null || dtDetail.Rows.Count == 0) ? string.Empty : "出库了" + dtDetail.Rows.Count + "个货物。");
                }
                else if (intFormType == 1)
                {
                    strMessage = "出库单更新成功！";// + ((dtDetail == null || dtDetail.Rows.Count == 0) ? string.Empty : "影响了" + dtDetail.Rows.Count + "个货物。");
                }
                else if (intFormType == 2)
                {
                    strMessage = "出库单删除成功！";// +((dtDetail == null || dtDetail.Rows.Count == 0) ? string.Empty : "删除了" + dtDetail.Rows.Count + "个货物。");
                }
                #endregion 编辑提示信息

                // 1、Head更新实行
                if (strUpdateSQL.Trim() != string.Empty)
                {
                    datacon.getcom(strUpdateSQL);
                }

                // 2、明细删除实行
                if (strDeleteSQL.Trim() != string.Empty)
                {
                    datacon.getcom(strDeleteSQL);
                }

                // 3、明细插入实行
                if (strInsertSQL.Trim() != string.Empty)
                {
                    datacon.getcom(strInsertSQL);
                }

                // 提示信息
                MessageBox.Show(strMessage);
                // 关闭画面
                this.Close();

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
        #endregion 保存按钮点击事件

        #region 货物种类明细事件
        #region 货物种类明细Cell进入事件（CellEnter）
        /// <summary>
        /// 货物种类明细Cell进入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvStockOutGoods_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.blnLoadFlg)
            {
                return;
            }

            if (e.ColumnIndex == -1 || e.RowIndex == -1)
            {
                return;
            }

            if (utils.ObjToString(this.dgvStockOutGoods.Rows[e.RowIndex].Cells[(int)GOODS_COL_NAME.GOODS_CODE].Value) == string.Empty)
            {
                // 显示空白
                this.GetBarCode(string.Empty);
            }
            else
            {
                this.GetBarCode(this.dgvStockOutGoods.Rows[e.RowIndex].Cells[(int)GOODS_COL_NAME.GOODS_CODE].Value.ToString());
            }
        }
        #endregion 货物种类明细Cell进入事件（CellEnter）

        #region 货物编号变更事件（CellValidating）
        /// <summary>
        /// 货物编号输入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvStockOutGoods_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (this.blnLoadFlg)
            {
                return;
            }

            //TODO: 加上初始化时不做处理的代码
            string strGoodsCode = string.Empty;
            DataRow dr;
            DataTable dtGoods;

            if (e.ColumnIndex == (int)GOODS_COL_NAME.GOODS_CODE)
            {
                // 修改前的Code
                string strBeforeGOODS_CODE = utils.ObjToString(this.dgvStockOutGoods.Rows[e.RowIndex].Cells[(int)GOODS_COL_NAME.GOODS_CODE].Value);
                // 现在的Code
                string strNowGOODS_CODE = utils.ObjToString(e.FormattedValue.ToString());

                if (strNowGOODS_CODE != string.Empty)
                {
                    if (strBeforeGOODS_CODE != strNowGOODS_CODE && !this.CheckGoodsCode(strNowGOODS_CODE, e.RowIndex))
                    {
                        e.Cancel = true;
                        return;
                    }

                    // 更换了货物Code
                    if (strBeforeGOODS_CODE != strNowGOODS_CODE)
                    {
                        // 既存货物删除处理
                        this.DeleteGoods(strBeforeGOODS_CODE, e.RowIndex);

                        dtGoods = datacon.getds("SELECT GOODS_CODE,GOODS_NAME,GOODS_MADEPLACE FROM T_GOODS_HEAD WHERE GOODS_CODE='" + e.FormattedValue.ToString() + "'", "T_GOODS_HEAD").Tables["T_GOODS_HEAD"];

                        if (dtGoods != null && dtGoods.Rows.Count > 0)
                        {
                            dr = dtGoods.Rows[0];
                            this.dgvStockOutGoods.Rows[e.RowIndex].Cells[(int)GOODS_COL_NAME.GOODS_NAME].Value = utils.ObjToString(dr["GOODS_NAME"]);
                            this.dgvStockOutGoods.Rows[e.RowIndex].Cells[(int)GOODS_COL_NAME.GOODS_MADEPLACE].Value = utils.ObjToString(dr["GOODS_MADEPLACE"]);

                        }
                        else
                        {
                            MessageBox.Show("不存在该货物，请重新输入!");
                            e.Cancel = true;
                            return;
                        }

                        this.GetBarCode(e.FormattedValue.ToString());
                    }

                }
                else if (strBeforeGOODS_CODE != string.Empty)
                {
                    // 既存货物删除处理
                    this.DeleteGoods(strBeforeGOODS_CODE, e.RowIndex);

                }
            }
        }
        #endregion 货物编号变更事件（CellValidating）

        #endregion 货物种类明细事件

        #region 序列号明细事件

        #region 序列号明细BarCode变更事件（UserAddedRow）
        /// <summary>
        /// 序列号明细BarCode变更事件（UserAddedRow）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvBarCode_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (this.blnLoadFlg)
            {
                return;
            }

            if (e.ColumnIndex == (int)BARCODE_COL_NAME.GOODS_BARCODE)
            {
                // 修改前的Code
                string strBeforeBarCode = utils.ObjToString(this.dgvBarCode.Rows[e.RowIndex].Cells[(int)BARCODE_COL_NAME.GOODS_BARCODE].Value);
                // 现在的Code
                string strNowBarCode = utils.ObjToString(e.FormattedValue.ToString());

                if (strNowBarCode.Contains(" "))
                {
                    MessageBox.Show("序列号不能含有空格！");
                    e.Cancel = true;
                    return;
                }
                else if (strNowBarCode != string.Empty)
                {
                    if (strNowBarCode != strBeforeBarCode && !this.CheckBarCode(strNowBarCode, e.RowIndex))
                    {
                        e.Cancel = true;
                        return;
                    }

                    if (strNowBarCode != strBeforeBarCode)
                    {
                        // 朗读序列号
                        Speech.SpeechString(strNowBarCode);

                        this.dgvBarCode.CurrentRow.Cells[(int)BARCODE_COL_NAME.GOODS_CODE].Value = this.dgvStockOutGoods.CurrentRow.Cells[(int)GOODS_COL_NAME.GOODS_CODE].Value;
                        this.dgvBarCode.CurrentRow.Cells[(int)BARCODE_COL_NAME.GOODS_NAME].Value = this.dgvStockOutGoods.CurrentRow.Cells[(int)GOODS_COL_NAME.GOODS_NAME].Value;
                        this.dgvBarCode.CurrentRow.Cells[(int)BARCODE_COL_NAME.GOODS_MADEPLACE].Value = this.dgvStockOutGoods.CurrentRow.Cells[(int)GOODS_COL_NAME.GOODS_MADEPLACE].Value;

                    }
                }
                else
                {
                    // 清除序列号明细显示数据
                    this.ClearBarCodeRowData(e.RowIndex);
                }
            }
        }
        #endregion 序列号明细BarCode变更事件（UserAddedRow）

        #endregion 序列号明细事件

        #region 语音设定按钮点击事件
        /// <summary>
        /// 语音设定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnVoice_Click(object sender, EventArgs e)
        {
            SysManage.frmVoiceSetting frmVoiceSetting = new SMS.SysManage.frmVoiceSetting();
            frmVoiceSetting.ShowDialog();
        }
        #endregion 语音设定按钮点击事件

        #endregion 事件

        #region 私有方法

        #region 设置画面数据
        /// <summary>
        /// 设置数据
        /// </summary>
        private void SetFormData()
        {
            this.dvStockOutGoods = new DataView(this.dtT_STOCK_OUT_DETAIL);
            this.dvStockOutGoodsDetail = new DataView(this.dtT_STOCK_OUT_DETAIL);

            // 出库单号
            this.tbxSTOCK_OUT_ID.Text = this.strSTOCK_OUT_ID;

            // 设置头部数据
            if (this.dtT_STOCK_OUT_HEAD != null &&
                this.dtT_STOCK_OUT_HEAD.Rows.Count == 1)
            {
                DataRow dr = this.dtT_STOCK_OUT_HEAD.Rows[0];

                // 出库日期
                this.dtpSTOCK_OUT_DATE.Value = Convert.ToDateTime(dr["STOCK_OUT_DATE"].ToString());
                // 操作者
                this.tbxSTOCK_OUT_OPERATER.Text = dr["STOCK_OUT_OPERATER"].ToString();
                // 确认者
                this.tbxSTOCK_OUT_CONFIRMER.Text = dr["STOCK_OUT_CONFIRMER"].ToString();
                // 备注
                this.tbxSTOCK_OUT_MEMO.Text = dr["STOCK_OUT_MEMO"].ToString();
            }
            else
            {

            }

            // 绑定上下两个明细数据
            this.BindDetail();

        }
        #endregion 设置画面数据

        #region 设置画面控件状态
        /// <summary>
        /// 设置画面控件状态
        /// </summary>
        private void SetControlStatus()
        {
            // 新建
            if (intFormType == 0)
            {
                // 出库单号
                this.tbxSTOCK_OUT_ID.Enabled = false;
                // 出库日期
                this.dtpSTOCK_OUT_DATE.Enabled = true;
                // 操作者
                this.tbxSTOCK_OUT_OPERATER.Enabled = true;
                // 确认者
                this.tbxSTOCK_OUT_CONFIRMER.Enabled = true;
                // 备注
                this.tbxSTOCK_OUT_MEMO.Enabled = true;
                // 按钮名称
                this.btnSave.Text = "新建";

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_CODE].ReadOnly = false;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_CODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_ENABLE;

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_NAME].ReadOnly = true;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_NAME].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_MADEPLACE].ReadOnly = true;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_MADEPLACE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_CODE].ReadOnly = true;
                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_CODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_BARCODE].ReadOnly = false;
                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_BARCODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_ENABLE;

            }
            // 变更
            else if (intFormType == 1)
            {
                // 出库单号
                this.tbxSTOCK_OUT_ID.Enabled = false;
                // 出库日期
                this.dtpSTOCK_OUT_DATE.Enabled = true;
                // 操作者
                this.tbxSTOCK_OUT_OPERATER.Enabled = true;
                // 确认者
                this.tbxSTOCK_OUT_CONFIRMER.Enabled = true;
                // 备注
                this.tbxSTOCK_OUT_MEMO.Enabled = true;
                // 按钮名称
                this.btnSave.Text = "变更";

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_CODE].ReadOnly = false;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_CODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_ENABLE;

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_NAME].ReadOnly = true;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_NAME].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_MADEPLACE].ReadOnly = true;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_MADEPLACE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_CODE].ReadOnly = true;
                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_CODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_BARCODE].ReadOnly = false;
                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_BARCODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_ENABLE;

            }
            // 删除
            else if (intFormType == 2)
            {
                // 出库单号
                this.tbxSTOCK_OUT_ID.Enabled = false;
                // 出库日期
                this.dtpSTOCK_OUT_DATE.Enabled = false;
                // 操作者
                this.tbxSTOCK_OUT_OPERATER.Enabled = false;
                // 确认者
                this.tbxSTOCK_OUT_CONFIRMER.Enabled = false;
                // 备注
                this.tbxSTOCK_OUT_MEMO.Enabled = false;
                // 按钮名称
                this.btnSave.Text = "删除";

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_CODE].ReadOnly = true;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_CODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_NAME].ReadOnly = true;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_NAME].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_MADEPLACE].ReadOnly = true;
                this.dgvStockOutGoods.Columns[(int)GOODS_COL_NAME.GOODS_MADEPLACE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_CODE].ReadOnly = true;
                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_CODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;

                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_BARCODE].ReadOnly = true;
                this.dgvBarCode.Columns[(int)BARCODE_COL_NAME.GOODS_BARCODE].DefaultCellStyle.BackColor = CommonConst.BACKCOLOR_DISABLE;
            }
        }
        #endregion 设置画面控件状态

        #region 设置画面控件状态和数据
        /// <summary>
        /// 设置画面控件状态和数据
        /// </summary>
        private void SetForm()
        {
            this.SetControlStatus();
            this.GetFormData();
            this.SetFormData();
        }
        #endregion 设置画面控件状态和数据

        #region 取得出库单信息
        /// <summary>
        /// 取得出库单信息
        /// </summary>
        private void GetFormData()
        {
            // 取得Head部数据
            this.dtT_STOCK_OUT_HEAD = datacon.getds("SELECT STOCK_OUT_ID,STOCK_OUT_DATE,STOCK_OUT_QTY,STOCK_OUT_OPERATER,STOCK_OUT_CONFIRMER,STOCK_OUT_MEMO "
                               + "FROM T_STOCK_OUT_HEAD WHERE STOCK_OUT_ID='" + this.strSTOCK_OUT_ID + "'", "T_STOCK_OUT_HEAD").Tables["T_STOCK_OUT_HEAD"];

            // 取得Detail部数据
            this.dtT_STOCK_OUT_DETAIL = datacon.getds("SELECT TSID.GOODS_CODE,TGH.GOODS_NAME,TGH.GOODS_MADEPLACE,TSID.GOODS_BARCODE"
                               + " FROM T_STOCK_OUT_DETAIL TSID INNER JOIN T_STOCK_OUT_HEAD TSIH ON TSID.STOCK_OUT_ID = TSIH.STOCK_OUT_ID"
                               + " INNER JOIN T_GOODS_HEAD TGH ON TGH.GOODS_CODE = TSID.GOODS_CODE"
                               + " WHERE TSID.STOCK_OUT_ID='" + this.strSTOCK_OUT_ID + "' ORDER BY TSID.GOODS_CODE ASC", "T_STOCK_OUT_DETAIL").Tables["T_STOCK_OUT_DETAIL"];

        }
        #endregion 取得出库单信息

        #region 取得序列号明细数据
        /// <summary>
        /// 取得序列号明细数据
        /// </summary>
        /// <param name="strGoodsCode"></param>
        private void GetBarCode(string strGoodsCode)
        {
            if (strGoodsCode == string.Empty)
            {
                strGoodsCode = "ERR12345678901234567890";
                this.dgvBarCode.AllowUserToAddRows = false;
            }
            else
            {
                this.dgvBarCode.AllowUserToAddRows = true;
            }

            this.dvStockOutGoodsDetail.RowFilter = "GOODS_CODE='" + strGoodsCode + "'";
            this.dgvBarCode.DataSource = this.dvStockOutGoodsDetail;
        }
        #endregion 取得序列号明细数据

        #region 货物删除处理
        /// <summary>
        /// 货物删除处理
        /// </summary>
        /// <param name="strGoodsID"></param>
        private void DeleteGoods(string strGOODS_CODE, int intRowIndex)
        {
            // 删除货物种类明细的行数据
            this.ClearGoodsRowData(intRowIndex);

            // 删除DataTable中的原始数据（多条）
            DataRow[] drs = this.dtT_STOCK_OUT_DETAIL.Select("GOODS_CODE='" + strGOODS_CODE + "'");

            if (drs != null)
            {
                foreach (DataRow dr in drs)
                {
                    dr.Delete();
                }
            }

            // 根据货物种类明细第一行数据重置序列号的明细
            this.GetBarCode(utils.ObjToString(this.dgvStockOutGoods.Rows[0].Cells[(int)GOODS_COL_NAME.GOODS_CODE].Value));

        }
        #endregion 货物删除处理

        #region 清除货物种类明细显示数据
        /// <summary>
        /// 清除货物种类明细显示数据
        /// </summary>
        /// <param name="intRowIndex"></param>
        private void ClearGoodsRowData(int intRowIndex)
        {
            this.dgvStockOutGoods.Rows[intRowIndex].Cells[(int)GOODS_COL_NAME.GOODS_CODE].Value = DBNull.Value;
            this.dgvStockOutGoods.Rows[intRowIndex].Cells[(int)GOODS_COL_NAME.GOODS_NAME].Value = DBNull.Value;
            this.dgvStockOutGoods.Rows[intRowIndex].Cells[(int)GOODS_COL_NAME.GOODS_MADEPLACE].Value = DBNull.Value;
        }
        #endregion 清除货物种类明细显示数据

        #region 清除序列号明细显示数据
        /// <summary>
        /// 清除序列号明细显示数据
        /// </summary>
        /// <param name="intRowIndex"></param>
        private void ClearBarCodeRowData(int intRowIndex)
        {
            DataRowView drv = (DataRowView)this.dgvBarCode.Rows[intRowIndex].DataBoundItem;

            if (drv != null)
            {
                drv.Delete();
            }
        }
        #endregion 清除序列号明细显示数据

        #region 货物种类明细的货物ID重复校验
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strGoodsId"></param>
        private bool CheckGoodsCode(string strGoodsCode, int intRowIndex)
        {
            if (strGoodsCode.Trim() != string.Empty)
            {
                for (int i = 0; i < this.dgvStockOutGoods.Rows.Count; i++)
                {
                    if (intRowIndex == i)
                    {
                        continue;
                    }

                    if (utils.ObjToString(this.dgvStockOutGoods.Rows[i].Cells[(int)GOODS_COL_NAME.GOODS_CODE].Value).Trim() == strGoodsCode.Trim())
                    {
                        MessageBox.Show("相同货物已存在！");
                        return false;
                    }
                }
            }

            return true;
        }
        #endregion 货物种类明细的货物ID

        #region 序列号明细的货物序列号重复校验
        /// <summary>
        /// 序列号明细的货物序列号重复校验
        /// </summary>
        /// <param name="strBarCode"></param>
        /// <param name="intRowIndex"></param>
        /// <returns></returns>
        private bool CheckBarCode(string strBarCode, int intRowIndex)
        {
            // 当前页面校验
            if (strBarCode.Trim() != string.Empty)
            {
                for (int i = 0; i < this.dgvBarCode.Rows.Count; i++)
                {
                    if (intRowIndex == i)
                    {
                        continue;
                    }

                    if (utils.ObjToString(this.dgvBarCode.Rows[i].Cells[(int)BARCODE_COL_NAME.GOODS_BARCODE].Value).Trim() == strBarCode.Trim())
                    {
                        MessageBox.Show("该序列号在当前列表中已存在，不可重复输入！");
                        return false;
                    }
                }
            }

            // 全部货物页面校验
            for (int i = 0; i < this.dtT_STOCK_OUT_DETAIL.Rows.Count; i++)
            {
                if (this.dtT_STOCK_OUT_DETAIL.Rows[i].RowState == DataRowState.Deleted)
                {
                    //TODO: 如果当前输入的BarCode，与本张出库单中已经被删除的BarCode重复，则一定不会与数据库
                    if (strBarCode.Trim() == this.dtT_STOCK_OUT_DETAIL.Rows[i]["GOODS_BARCODE", DataRowVersion.Original].ToString().Trim())
                    {
                        return true;
                    }
                }
                else if (strBarCode.Trim() == this.dtT_STOCK_OUT_DETAIL.Rows[i]["GOODS_BARCODE"].ToString().Trim())
                {
                    MessageBox.Show("该序列号的货物在本张出库单中已存在，不可重复出库！");
                    return false;
                }
            }

            // 数据库校验（本张出库单以外）
            DataSet dsBarCode = datacon.getds("SELECT COUNT(GOODS_BARCODE) AS EXIST_FLG"
                                       + " FROM T_STOCK_OUT_DETAIL WHERE STOCK_OUT_ID <> '" + this.strSTOCK_OUT_ID
                                       + "' AND GOODS_BARCODE = '" + strBarCode + "';", "T_STOCK_OUT_DETAIL");

            if (dsBarCode != null && dsBarCode.Tables["T_STOCK_OUT_DETAIL"] != null &&
                dsBarCode.Tables["T_STOCK_OUT_DETAIL"].Rows.Count > 0 &&
                utils.ObjToString(dsBarCode.Tables["T_STOCK_OUT_DETAIL"].Rows[0]["EXIST_FLG"]) != "0")
            {
                MessageBox.Show("该序列号的货物在其他出库单中已存在，不可重复出库！");
                return false;
            }

            // 数据库校验（出库序列号在入库序列号中的存在性校验）
            dsBarCode = datacon.getds("SELECT COUNT(GOODS_BARCODE) AS EXIST_FLG"
                                       + " FROM T_STOCK_IN_DETAIL WHERE GOODS_BARCODE = '" + strBarCode + "';", "T_STOCK_IN_DETAIL");

            if (dsBarCode != null && dsBarCode.Tables["T_STOCK_IN_DETAIL"] != null &&
                dsBarCode.Tables["T_STOCK_IN_DETAIL"].Rows.Count > 0 &&
                utils.ObjToString(dsBarCode.Tables["T_STOCK_IN_DETAIL"].Rows[0]["EXIST_FLG"]) == "0")
            {
                MessageBox.Show("该序列号货物不在库！");
                return false;
            }

            return true;
        }
        #endregion 序列号明细的货物序列号重复校验

        #region 绑定上下两个明细数据
        /// <summary>
        /// 绑定上下两个明细数据
        /// </summary>
        private void BindDetail()
        {
            // 设置明细部数据
            if (this.dtT_STOCK_OUT_DETAIL != null &&
                this.dtT_STOCK_OUT_DETAIL.Rows.Count > 0)
            {
                // 货物种类的明细
                this.dvStockOutGoods.RowFilter = "";
                string[] columnNames = new string[] { "GOODS_CODE", "GOODS_NAME", "GOODS_MADEPLACE" };
                this.dgvStockOutGoods.DataSource = this.dvStockOutGoods.ToTable(true, columnNames);
                // 序列号的明细
                this.GetBarCode(utils.ObjToString(this.dgvStockOutGoods.Rows[0].Cells[(int)GOODS_COL_NAME.GOODS_CODE].Value));
            }
            else
            {
                // 货物种类明细没有数据时、序列号的明细设置为空白
                this.GetBarCode(string.Empty);
            }
        }
        #endregion 绑定上下两个明细数据



        #endregion 私有方法
    }
}
